//
//  UIImage+OXExtension.m
//  XHCoach
//
//  Created by 陈强 on 16/5/19.
//  Copyright © 2016年 com.apis. All rights reserved.
//

#import "UIImage+OXExtension.h"

@implementation UIImage (OXExtension)
- (UIImage *)circleImage
{
    // NO代表透明
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
    
    // 获得上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 添加一个圆
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    CGContextAddEllipseInRect(ctx, rect);
    
    // 裁剪
    CGContextClip(ctx);
    
    // 将图片画上去
    [self drawInRect:rect];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
}

+ (UIImage *)drawImageWithString:(NSString *)str Attributes:(NSDictionary *)attrs {
    
    CGRect rect = CGRectMake(0, 0, 55.f, 55.f);
    
    UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [[UIColor lightGrayColor] CGColor]);
    
    CGContextFillRect(context, rect);
    
    CGContextAddEllipseInRect(context, rect);
    
    // 裁剪
    CGContextClip(context);
    
    CGSize size = [str sizeWithAttributes:attrs];
    
    [str drawAtPoint:(CGPointMake((55 - size.width)*0.5, (55 - size.height)*0.5)) withAttributes:attrs];
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
@end
